package com.example.answer_staff;

import java.util.ArrayList;

import org.apache.http.Header;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.example.answer_staff.api.ApiManager;
import com.gaosai.staff.commlistadapter.ShowQuizAdapter;
import com.gaosai.staff.domain.MessageResult;
import com.gaosai.staff.utils.JsonUtils;
import com.gaosai.staff.xlistview.XListView;
import com.gaosai.staff.xlistview.XListView.IXListViewListener;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class QuizActivity extends BaseActivity implements IXListViewListener{
	private int page = 1 ;
	private int pagesize =10 ;
	private String keyWord="";
	private String tagNames="";
	private XListView listview;
	private ArrayList<String> showQuiz_list;
	private ShowQuizAdapter showQuizAdapter;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewItem(R.layout.quiz_listview) ;
		setupView();
		setListeneers();
		getActivityData();

	}

	private void getActivityData() {
		Log.e("quiz的tagnames", "tagNames是"+tagNames);
		ApiManager.getInstance().quizshow(QuizActivity.this,String.valueOf(page),String.valueOf(pagesize),keyWord,tagNames,new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				Log.e("eee", "QuizActivity开始向服务器请求数据");
				String result = new String(arg2) ;
				Log.e("Quiz的的的chengogn是sssssssss", result) ;
				MessageResult messageResult = MessageResult.prase(result) ;
				if(0==messageResult.getCode()){
					showQuiz_list.addAll(JsonUtils.JsonShowQuizResult(messageResult.getData())) ;
					showQuizAdapter.notifyDataSetChanged() ;
					System.out.println("showQuiz_list是"+showQuiz_list);
				}else{
					Toast.makeText(QuizActivity.this, messageResult.getMsg(), 0).show() ;
				}
			}
			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				Log.e("fffffffffffffffffffffff", "666666") ;
			}
		}) ;
	}

	private void setListeneers() {
		listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent=new Intent(QuizActivity.this,ListQuizDetailsActivity.class);
				//传的是服务器返回的值
				intent.putExtra("new", showQuiz_list.get(position-1));
				startActivity(intent);	
				startActivity(intent) ;
			}
		});




	}

	private void setupView() {
		listview=(XListView)findViewById(R.id.listView_quiz);
		listview.setPullRefreshEnable(true);
		listview.setPullLoadEnable(true);
		listview.setXListViewListener(this) ;
		showQuiz_list=new ArrayList<String>();
		showQuizAdapter = new ShowQuizAdapter(QuizActivity.this,showQuiz_list);
		listview.setAdapter(showQuizAdapter);
		setTitleName("Quiz") ;
		showAllImage(R.drawable.menu, 0, 0, null, null);
		hideTitlebtnRight();
		hideTitlebtnAlignRight();

	}



	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub

	}
}
